<template>
	<view>
		<view class="search-box">
			<!-- 基本用法 -->
			<uni-search-bar  @input="input" :radius="100" cancelButton="none" ></uni-search-bar>
		</view>
		
		<!-- 搜索结果列表显示 -->
		<view class="sugg-list" v-if="searchResult.length !== 0">
			<view class="sugg-item" v-for="(item, i) in searchResult" :key="i" @click="gotoDetail(item)">
				<view class="sugg-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 历史搜索 -->
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="item" v-for="(item, i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//定时器
				timer: null,
				//搜索关键词
				kw: null,
				//搜索结果列表
				searchResult: [],
				//历史搜索结果
				historyList: []
			};
		},
		//在 onLoad 生命周期函数中，加载本地存储的搜索历史记录：
		onLoad() {
		  this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			input(e) {
				//清除定时器
				clearTimeout(this.timer)
				//重新启动一个定时器，并把timerId赋值给 this.timer
				this.timer =  setTimeout(() =>{
					//如果500 毫秒内，没有触发新的输入，则为搜索关键词赋值
					this.kw = e.valueOf()
					
					this.getSearchList()
				}, 500)
			},
			
			async getSearchList() {
				if ( this.kw.length === 0 ) {
					this.searchResult = []
					return
				}
				
				const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResult = res.message
				//存储历史搜索纪录
				this.saveSearchHistory()
			},
			//点击搜索结果跳转商品详情页面
			gotoDetail(item) {
				uni.navigateTo({
					url:'../goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			
			saveSearchHistory() {
				//直接把搜索关键词 push到historyList数组中
				// this.historyList.push(this.kw)
				//将数组转化为set对象 可以解决数组中重复问题
				const set = new Set(this.historyList)
				//2.调用set对象的delete方法，移除对应的元素
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList = Array.from(set)
				
				// 调用 uni.setStorageSync(key, value) 将搜索历史记录持久化存储到本地
				 uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			//清空历史记录
			clean() {
				this.historyList = []
				uni.setStorageSync('kw', '[]')
			},
			
			gotoGoodsList(kw) {
				uni.navigateTo({
					url: '../goods_list/goods_list?query=' + kw
				})
			}
		},
		//计算属性
		computed: {
			histories() {
				//注意：由于数组时引用类型，所有不要直接至于原数组调用reverse方法，以免修改
				//原数组的顺序
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}
	.sugg-list{
		padding: 0 5px;
		.sugg-item{
			display: flex;
			padding: 10px 0;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1px solid #efefef;
			font-size: 12px;
			.sugg-name{
				//不换行
				white-space: nowrap;
				//溢出部分隐藏
				overflow: hidden;
				//文本溢出后，使用...代替
				text-overflow: ellipsis;
			}
		}
	}
	
	.history-box {
		border-bottom: 1px solid #efefef;
		padding-bottom: 10px;
		.history-title {
			display: flex;
			justify-content: space-between;
			padding: 5px 10px;
			font-size: 13px;
		}
		.history-list {
			
			uni-tag {
				margin: 0 10px;
			}
		}
	}
</style>
